package com.atguigu.daijia.driver.service.impl;

import com.atguigu.daijia.common.constant.RedisConstant;
import com.atguigu.daijia.common.execption.GuiguException;
import com.atguigu.daijia.common.result.Result;
import com.atguigu.daijia.common.result.ResultCodeEnum;
import com.atguigu.daijia.customer.client.CustomerInfoFeignClient;
import com.atguigu.daijia.dispatch.client.NewOrderFeignClient;
import com.atguigu.daijia.driver.client.DriverInfoFeignClient;
import com.atguigu.daijia.driver.service.DriverService;
import com.atguigu.daijia.map.client.LocationFeignClient;
import com.atguigu.daijia.model.form.driver.DriverFaceModelForm;
import com.atguigu.daijia.model.form.driver.UpdateDriverAuthInfoForm;
import com.atguigu.daijia.model.vo.driver.DriverAuthInfoVo;
import com.atguigu.daijia.model.vo.driver.DriverLoginVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Slf4j
@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class DriverServiceImpl implements DriverService {

    //注入远程调用接口
    @Autowired
    private DriverInfoFeignClient client;

    //redis
    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private LocationFeignClient locationFeignClient;

    @Autowired
    private NewOrderFeignClient newOrderFeignClient;

    /*
     * 微信小程序登录
     * */
    @Override
    public String login(String code) {
        //用code进行远程调用，返回用户id
        Result<Long> loginResult = client.login(code);
        //判断，如果返回失败，则返回错误的提示
        if (loginResult.getCode() != 200) {
            throw new GuiguException(ResultCodeEnum.DATA_ERROR);
        }
        //获取远程调用返回的用户id
        Long driverId = loginResult.getData();
        //判断返回的id是否为空，为空报错
        if (driverId == null) {
            throw new GuiguException(ResultCodeEnum.DATA_ERROR);
        }
        //生成token字符串
        String token = UUID.randomUUID().toString().replaceAll("-","");
        //把用户id放到redis存储，设置过期时间
        //key：user:login:+token  value： id
        redisTemplate.opsForValue().set(RedisConstant.USER_LOGIN_KEY_PREFIX+token,driverId.toString(),
                                             RedisConstant.USER_LOGIN_KEY_TIMEOUT, TimeUnit.HOURS);//设置过期时间
        //返回token字符串
        return token;
    }

    @Override
    public DriverAuthInfoVo getDriverAuthInfo(Long driverId) {
        return client.getDriverAuthInfo(driverId).getData();

    }

    /*
    * 更新司机认证信息
    * */
    @Override
    public Boolean updateDriverAuthInfo(UpdateDriverAuthInfoForm updateDriverAuthInfoForm) {
        return client.updateDriverAuthInfo(updateDriverAuthInfoForm).getData();

    }

    /*
    * 创建司机人脸识别模型
    * */
    @Override
    public Boolean creatDriverFaceModel(DriverFaceModelForm driverFaceModelForm) {
        return client.creatDriverFaceModel(driverFaceModelForm).getData();
    }

    /*
     * 判断司机当日是否进行过人脸识别
     * */
    @Override
    public Boolean isFaceRecognition(Long driverId) {
        return client.isFaceRecognition(driverId).getData();
    }

    /*
     * 人脸识别认证
     * */
    @Override
    public Boolean verifyDriverFace(DriverFaceModelForm driverFaceModelForm) {
        return client.verifyDriverFace(driverFaceModelForm).getData();

    }

    /*
     * 开启接单服务
     * */
    @Override
    public Boolean startService(Long driverId) {
        //判断是否完成认证
        Result<DriverLoginVo> driverInfo = client.getDriverInfo(driverId);
        DriverLoginVo driverLoginVo = driverInfo.getData();
        if(driverLoginVo.getAuthStatus() != 2){
            throw new GuiguException(ResultCodeEnum.AUTH_ERROR);
        }
        //判断当日是否完成人脸识别
        Boolean isFace = client.isFaceRecognition(driverId).getData();
        if(!isFace){
            throw new GuiguException(ResultCodeEnum.FACE_ERROR);
        }
        //更新状态值 - 接单状态 - 1
        client.updateServiceStatus(driverId,1);
        //删除redis司机位置信息
        locationFeignClient.removeDriverLocation(driverId);
        //清空司机临时队列数据
        newOrderFeignClient.clearNewOrderQueueData(driverId);
        return true;
    }

    /*
     * 停止接单服务
     * */
    @Override
    public Boolean stopService(Long driverId) {
        //更新司机接单状态
        client.updateServiceStatus(driverId,0);
        //删除redis司机位置信息
        locationFeignClient.removeDriverLocation(driverId);
        //清空司机临时队列数据
        newOrderFeignClient.clearNewOrderQueueData(driverId);
        return true;
    }

}
